package com.dty.common.utils;

import java.io.File;

/**
 * @ClassName RootPathUtils
 * @Description 项目运行路径
 * @Author 杨钧生
 * @Date 2020/12/21 12:39
 */
public class RootPathUtils {

    private static String RUN_PATH;

    // 初始化日志路径
    static {
        String filePath = System.getProperty("java.class.path");
        String pathSplit = System.getProperty("path.separator");//得到当前操作系统的分隔符，windows下是";",linux下是":"
        if (filePath.contains(pathSplit)) {
            filePath = filePath.substring(0, filePath.indexOf(pathSplit));
        } else if (filePath.endsWith(".jar")) {//截取路径中的jar包名,可执行jar包运行的结果里包含".jar"
            filePath = filePath.substring(0, filePath.lastIndexOf(File.separator) + 1);
        }

        if (filePath.contains("target")) {
            RUN_PATH = filePath.substring(0, filePath.indexOf("target")) + "logs\\";
        } else {
            RUN_PATH = filePath + "logs\\";
        }

    }

    /**
     * 项目运行根路径
     *
     * @return String 路径
     */
    public static String getRootPath() {
        return RUN_PATH;
    }

}
